// https://leetcode.cn/problems/combination-sum-iii/

// 1-9不存在重复，所以不需要额外去重
class Solution {
public:
    void dfs(vector<vector<int>>& res, vector<int>& tmp, int k, int n, int start, int sum) {
        if (sum >= n) {
            if (sum == n && tmp.size() == k) res.push_back(tmp);
            return;
        }
        for (int i = start; i <= 9; ++i) {
            if (i > n) return;
            tmp.push_back(i);
            sum += i;
            dfs(res, tmp, k, n, i + 1, sum); // 不可以重复使用，传入i+1
            tmp.pop_back();
            sum -= i; 
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<vector<int>> res;
        vector<int> tmp;
        dfs(res, tmp, k, n, 1, 0);
        return res;
    }
};